package com.example.doctorsystem.dao;

import com.example.doctorsystem.model.Prescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PrescriptionDaoImpl implements PrescriptionDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int createPrescription(Prescription p) {
        String sql = "INSERT INTO prescriptions (doctor_id, doctor_name, patient_id, patient_name, medicine_name, amount) VALUES (?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, p.getDoctorId(), p.getDoctorName(), p.getPatientId(), p.getPatientName(), p.getMedicineName(), p.getAmount());
    }

    @Override
    public List<Prescription> getAllPrescriptions() {
        return jdbcTemplate.query("SELECT * FROM prescriptions", new BeanPropertyRowMapper<>(Prescription.class));
    }

    @Override
    public List<Prescription> getPrescriptionsByDoctorId(int doctorId) {
        return jdbcTemplate.query("SELECT * FROM prescriptions WHERE doctor_id = ?", new BeanPropertyRowMapper<>(Prescription.class), doctorId);
    }

    @Override
    public List<Prescription> getPrescriptionsByPatientId(int patientId) {
        return jdbcTemplate.query("SELECT * FROM prescriptions WHERE patient_id = ?", new BeanPropertyRowMapper<>(Prescription.class), patientId);
    }

    @Override
    public Prescription getById(int id) {
        String sql = "SELECT * FROM prescriptions WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Prescription.class), id);
    }

    @Override
    public int deletePrescription(int id) {
        return jdbcTemplate.update("DELETE FROM prescriptions WHERE id = ?", id);
    }
}
